package ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author TWD
 */
@Stateless
public class StakeholderAccountsBean implements StakeholderAccountsBeanRemote {
    @PersistenceContext()
    EntityManager em;
    
    private StakeholderEntity stakeholder;
    private StakeholderTierEntity tier;
    
    public StakeholderAccountsBean(){}
    
    public boolean createStakeholder(String name, Integer loyaltyPoints){
        boolean status;
        tier = em.find(StakeholderTierEntity.class, "Bronze");
        status = stakeholder.create(name, loyaltyPoints, tier);
        em.persist(stakeholder);       
        return status;
    }
    
    public boolean deleteStakeholder(Long id){      
        stakeholder = em.find(StakeholderEntity.class, id);
        if(stakeholder == null){
            return false;
        }else{
            em.remove(stakeholder);
            return true;
        }
    }
    
    public boolean changeStakeholderTier(String stringTier, Long id){
        try{
            
            tier = em.find(StakeholderTierEntity.class, stringTier);
            stakeholder = em.find(StakeholderEntity.class, id);
            stakeholder.setStakeholderTier(tier);
            em.flush();
            return true;
        
        }catch(Exception ex){
            return false;
        }       
    }

    @Override
    public boolean createStakeholder(String name, Integer loyaltyPoints, StakeholderTierEntity stakeholderTier, List<AdvertisementEntity> Advertisement, List<StakeholderBillEntity> stakeholderBill) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
